﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using XsqTech.SmartOA.Dtos.DTO;
using XsqTech.SmartOA.Dtos.XSPDto.FromDto;
using XsqTech.SmartOA.IService;
using XsqTech.SmartOA.Service;
using XsqTech.SmartOA.Service.DTO;

namespace XsqTech.SmartOA.Application.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class FormController : ControllerBase
    {
        /// <summary>
        /// 依赖注入
        /// </summary>
        IFormService _formService;

        public FormController(IFormService formService)
        {
            _formService = formService;
        }
        /// <summary>
        /// 表单添加
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddFrom(AddFromDto dto)
        {
            if (!ModelState.IsValid)
            {
                return Ok(new ResponseModel() { Code = State.数据未导入, Message = "数据未导入" });
            }
            var data = _formService.AddFrom(dto);
            if (data.Count > 0)
            {
                return Ok(data);
            }
            else
            {
                return Ok(data);
            }
        }

        /// <summary>
        /// 设计表单分页显示
        /// </summary>
        /// <param name="Name"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult FormQuery(string? Name, int pageIndex, int pageSize)
        {
            if (pageIndex <= 0)
            {
                pageIndex = 1;
            }
            if (pageSize <= 0)
                pageSize = 10;

            return Ok(_formService.FormQuery(Name, pageIndex, pageSize));
        }
        /// <summary>
        /// 表单删除（单删）
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult FormDelete(string id)
        {
            var data = _formService.FormDelete(id);
            return Ok(data);
        }
        /// <summary>
        /// 表单删除（批删）
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpDelete("all")]
        public IActionResult AllFormDelete(List<AddFromDto> dto)
        {
            var data = _formService.AllFormDelete(dto);
            return Ok(data);
        }
        /// <summary>
        /// 表单反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("id")]
        public IActionResult FormQueryId(string id) {
            return Ok(_formService.FormQueryId(id));
        }
        /// <summary>
        /// 表单修改
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdateFrom(AddFromDto dto)
        {
            var data = _formService.UpdateFrom(dto);
            return Ok(data);
        }
    }
}
